/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shellCommands;

import Sessions.EcomCustomersRemote;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author SOGOBA
 */
public class ConnectCommand implements InterShell {
    ContextCommande cxtCmd;
   
    @Override
    public Commands getName() {
        return Commands.connect;
    }

    public ConnectCommand (ContextCommande ContextCommand){
          cxtCmd =  ContextCommand;
    }

    @Override
    public String getShortDescription() {
        return "connect client to the system \n";

    }

    @Override
    public void execute(String cmd) throws Exception {
           try {
            // StringBuffer res = new StringBuffer();
            InitialContext sc = new InitialContext();
            EcomCustomersRemote ecomCustomerBean = (EcomCustomersRemote) sc.lookup("Sessions.EcomCustomersRemote");
            StringTokenizer st = new StringTokenizer(cmd, " ");
            // Ignore la commande
            st.nextToken();
            // sans argument, on liste les produits du caddie en cours
            if (st.countTokens() == 2) {
                String name = st.nextToken();
                String pwd= st.nextToken();
                ecomCustomerBean.getConnect(name, pwd);
                cxtCmd.setVar("state", true);
                System.out.println("You are now connect to the system");
            }
        }
            catch (NamingException ex) {
            Logger.getLogger(CartCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


